<?php

namespace App\Controllers\Diary;

use App\Models\Diary\Topic;

class GetActivityDetail extends \App\Libs\Base\BaseAction
{
    protected function beforeRun()
    {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }

    /**
     * @return array 根据活动的期数(period)，返回活动的详情数据；
     */
    public function execute()
    {
        $pointedPeriod = $this->request->get('period', 'int', 0);
        if ($pointedPeriod == 0) {
            return \App\Libs\Views\Json::view(0, '缺少参数period', null);
        }
        //todo:aid是什么鬼
        $pointedPeriodData = Topic::find(array(
            'conditions' => 'period = ?1',
            'columns' => 'topic_logo,topic_title,topic_sum_comment,period,topic_content,aid,editor,ishot,ctime',
            'bind' => array(1 => $pointedPeriod)
        ));
        if (empty($pointedPeriodData)) {
            return \App\Libs\Views\Json::view(1, '没有此项活动', null);
        }
        //转换期数为"最新"或者"第几期"
        $topicModel = new Topic();
        $period = $topicModel->getNewlestPeriodNum();
        if ($period == false) {
            return \App\Libs\Views\Json::view(1, '没有开启任何活动的显示', null);
        }
        if ($pointedPeriodData->period == $period) {
            $pointedPeriodData->period = '最新';
        } else {
            $pointedPeriodData->period = '第' . $pointedPeriodData->period . '期';
        }
        $pointedPeriodData->ctime = date('m.d.Y',strtotime($pointedPeriodData->ctime));
        return \App\Libs\Views\Json::view(0, '成功', $pointedPeriodData);



    }
}
